#切换目录
cd $(cd "$(dirname "$0")" && pwd)

#输出帮助文档
function printHelp() {
    echo "-clear                    清空全部设置"
    echo "-Path                     orderer 与 cryptogen 命令位置"
    echo "-channelID                通道名称"
    echo "-chaincode_sequence       链码序列号"
    echo "-chaincode_name           链码名称"
    echo "-chaincode_lang           链码语言"
    echo "-chaincode_label          链码标签"
    echo "-chaincode_path           本地链码存储位置"
    echo "-chaincode_URL            链码地址"
    echo "-chaincode_version        链码版本"
    echo "-MSPID                    MSPID"
    echo "-core                     core.yaml 文件位置"
    echo "-default_config           配置文件位置"
    echo "-order_path               order 节点 order.sh 脚本路径"
    echo "-h                        打印此帮助界面"
    echo
    echo
    echo "-query                    查询链码"
    echo "-invoke                   调用链码"
    echo "-approveCC                审议链码"
    echo "-installCC                安装链码"
    echo "-packageCC                打包链码"
    echo "-deployCC                 部署链码"
    echo "-setAP                    设置锚节点"
    echo "-deployPeer               部署节点"
    echo "-getCI                    获取通道信息"
    echo "-getDP                    环境变量配置命令"
    echo
    echo
    echo "当前各属性默认参数"
    echo "-Path                     ${path}"
    echo "-channelID                ${channelID}"
    echo "-chaincode_sequence       ${chaincode_sequence}"
    echo "-chaincode_name           ${chaincode_name}"
    echo "-chaincode_lang           ${chaincode_lang}"
    echo "-chaincode_label          ${chaincode_label}"
    echo "-chaincode_path           ${chaincode_path}"
    echo "-chaincode_URL            ${chaincode_URL}"
    echo "-chaincode_version        ${chaincode_version}"
    echo "-MSPID                    ${MSPID}"
    echo "-core                     ${core}"
    echo "-default_config           ${default_config}"
    echo "-order_path               ${order_path}"
}

#替换指定文件内容
function replaceContent() {
    sed -i "s/$2/$3/g" $1
}

#检查返回值
function checkRet() {
    if [ $1 != "0" ]; then
        echo
        echo "程序异常结束"
        exit 1
    fi
}

#读取包ID
function readPackageId() {
    recent_package_id=$(cat ${root}/package.id)
    recent_package_id=$(echo ${recent_package_id// /} | tr -d '\r')
}

#写包ID
function writePackageId() {
    echo $1 >${root}/package.id
}

#判断文件是否存在
function checkFile() {
    if [ ! -f $1 ]; then
        echo "文件 $1 不存在！"
        echo "file $1 not exist!"
        echo $2
        exit 1
    fi
}

#清空已存在目录
function checkDiction() {
    #目录清空
    if [ -d $1 ]; then
        rm -rf $1
    fi
    mkdir -p $1

    #将文件移动至文件夹
    for i in "$@"; do
        if [ "${i}" = "$1" ]; then
            continue
        fi

        cp ${i} $1
    done
}

#获取 peer 节点组织
function getOrg() {
    pos=$(expr index "$1" ".")
    org_name=${1:${pos}}
}

#获取 peer 节点组织
function getOrderOrg() {
    pos=$(expr index "$1" ".")
    order_org_name=${1:${pos}}
}

#为第一个节点启动 peer
function deployFirstPeer() {
    #判断文件是否存在
    checkFile $core "未找到 peer 配置文件，无法启动 peer"

    #判断目录是否存在
    checkDiction "./organizations"
    checkDiction "./organizations/peerOrganizations"
    checkDiction "./organizations/tlscacerts"
    checkDiction "./peer" $core
    checkDiction "./peer/channel-artifacts"

    #替换端口设置
    replaceContent "./peer/$core" '$port' "${peer_port}"

    echo
    echo "正在从 order 节点下载当前组织信息，请输入 order 节点系统管理员密码"
    scp -r "root@${order_name}:${order_path}/organizations/peerOrganizations/${org_name}" ./organizations/peerOrganizations/
    checkRet $?

    cd peer
    export FABRIC_CFG_PATH=$PWD

    echo
    echo "正在启动 peer 节点"
    peer node start >>log_peer.log 2>&1 &
    checkRet $?

    sleep 2s
    cat log_peer.log

    echo "已启动 peer 节点, 请检查日志是否正常"
    sleep 3s

    echo
    echo "正在从 order 节点下载通信证书，请输入 order 节点系统管理员密码"
    scp -r "root@${order_name}:${order_path}/order/channel-artifacts" ./
    checkRet $?

    echo
    echo "正在从 order 节点下载所有组织证书，请输入 order 节点系统管理员密码"
    scp -r "root@${order_name}:${order_path}/organizations/tlscacerts" ../organizations/
    checkRet $?

    echo
    echo "正在从 order 节点下载 tls ca 证书，请输入 order 节点系统管理员密码"
    scp -r "root@${order_name}:${order_path}/organizations/ordererOrganizations/${order_org_name}/orderers/${order_name}/msp/tlscacerts/tlsca.${order_org_name}-cert.pem" ../organizations/tlscacerts
    checkRet $?

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    peer channel create -o ${order_name}:${order_port} -c ${channelID} -f ./channel-artifacts/${channelID}.tx --outputBlock ./channel-artifacts/${channelID}_${org_name}.block --tls --cafile $ORDERER_TLSCA
    checkRet $?

    peer channel join -b ./channel-artifacts/${channelID}_${org_name}.block
    checkRet $?

    echo
    getChannelInfo

    echo "操作已完成，请检查区块信息是否正常"
}

#为其他节点启动 peer
function deployOtherPeer() {
    #判断文件是否存在
    checkFile $core "未找到 peer 配置文件，无法启动 peer"

    #判断目录是否存在
    checkDiction "./organizations"
    checkDiction "./organizations/peerOrganizations"
    checkDiction "./organizations/tlscacerts"
    checkDiction "./peer" $core
    checkDiction "./peer/channel-artifacts"

    #替换端口设置
    replaceContent "./peer/$core" '$port' "${peer_port}"

    echo
    echo "正在从 order 节点下载当前组织信息，请输入 order 节点系统管理员密码"
    scp -r "root@${order_name}:${order_path}/organizations/peerOrganizations/${org_name}" ./organizations/peerOrganizations/
    checkRet $?

    echo
    echo "正在从 order 节点下载所有组织证书，请输入 order 节点系统管理员密码"
    scp -r "root@${order_name}:${order_path}/organizations/tlscacerts" ./organizations
    checkRet $?

    echo
    echo "正在从 order 节点下载 tls ca 证书，请输入 order 节点系统管理员密码"
    scp -r "root@${order_name}:${order_path}/organizations/ordererOrganizations/${order_org_name}/orderers/${order_name}/msp/tlscacerts/tlsca.${order_org_name}-cert.pem" ./organizations/tlscacerts
    checkRet $?

    cd peer
    export FABRIC_CFG_PATH=$PWD

    echo
    echo "正在启动 peer 节点"
    peer node start >>log_peer.log 2>&1 &
    checkRet $?

    sleep 2s
    cat log_peer.log

    echo "已启动 peer 节点, 请检查日志是否正常"
    sleep 3s

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    peer channel fetch 0 ./channel-artifacts/${channelID}_${org_name}.block -o ${order_name}:${order_port} -c ${channelID} --tls --cafile $ORDERER_TLSCA
    checkRet $?

    peer channel join -b ./channel-artifacts/${channelID}_${org_name}.block
    checkRet $?

    echo
    getChannelInfo

    echo "操作已完成，请检查区块信息是否正常"
}

#设置锚节点
function setAnchorPeer() {
    #安装 jq 工具
    sudo apt install jq -y

    cd peer

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    peer channel fetch config channel-artifacts/${channelID}_config_block.pb -o ${order_name}:${order_port} -c ${channelID} --tls --cafile $ORDERER_TLSCA
    checkRet $?

    cd channel-artifacts

    configtxlator proto_decode --input ${channelID}_config_block.pb --type common.Block --output config_block.json
    checkRet $?
    jq .data.data[0].payload.data.config config_block.json >config.json
    checkRet $?
    cp config.json config_copy.json
    checkRet $?

    jq '.channel_group.groups.Application.groups.'${MSPID}'.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "'${peer_name}'","port": '${peer_port}'}]},"version": "0"}}' config_copy.json >modified_config.json
    checkRet $?

    configtxlator proto_encode --input config.json --type common.Config --output config.pb
    checkRet $?
    configtxlator proto_encode --input modified_config.json --type common.Config --output modified_config.pb
    checkRet $?
    configtxlator compute_update --channel_id ${channelID} --original config.pb --updated modified_config.pb --output config_update.pb
    checkRet $?

    configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate --output config_update.json
    checkRet $?
    echo '{"payload":{"header":{"channel_header":{"channel_id":"'${channelID}'", "type":2}},"data":{"config_update":'$(cat config_update.json)'}}}' | jq . >config_update_in_envelope.json
    checkRet $?
    configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope --output config_update_in_envelope.pb
    checkRet $?

    cd ..
    peer channel update -f channel-artifacts/config_update_in_envelope.pb -c ${channelID} -o ${order_name}:${order_port} --tls --cafile $ORDERER_TLSCA
    checkRet $?

    echo
    getChannelInfo

    echo "操作已完成，请检查区块信息是否正常"
}

#审议链码
function approveCC() {
    cd ${root}/peer

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    echo
    echo "正在提交链码审议"
    readPackageId
    peer lifecycle chaincode approveformyorg -o ${order_name}:${order_port} --channelID ${channelID} --name ${chaincode_name} --version ${chaincode_version} --package-id ${recent_package_id} --sequence ${chaincode_sequence} --tls --cafile $ORDERER_TLSCA
    checkRet $?

    echo
    echo "已提交链码审议"

    #等待其他节点审议链码
    while true; do
        echo
        echo "输入(Q)以查询当前审议状况"
        echo "输入(Y)使用当前节点提交链码"
        echo "输入(S)以结束"

        read line

        case $line in
        Q)
            echo
            echo
            peer lifecycle chaincode checkcommitreadiness --channelID ${channelID} --name ${chaincode_name} --version ${chaincode_version} --sequence ${chaincode_sequence} --tls --cafile $ORDERER_TLSCA --output json
            checkRet $?
            ;;
        Y)
            echo
            echo "正在提交链码"

            args=""
            for j in $(ls ../organizations/tlscacerts -1); do
                if [ ${j} != "tlsca.${order_org_name}-cert.pem" ]; then
                    args=${args}" --peerAddresses ${j} --tlsRootCertFiles ../organizations/tlscacerts/${j}"
                fi
            done
            peer lifecycle chaincode commit -o ${order_name}:${order_port} --channelID ${channelID} --name ${chaincode_name} --version ${chaincode_version} --sequence ${chaincode_sequence} --tls --cafile $ORDERER_TLSCA ${args}
            checkRet $?

            echo
            echo
            peer lifecycle chaincode querycommitted --channelID ${channelID} --name ${chaincode_name} --cafile $ORDERER_TLSCA
            checkRet $?

            echo "链码已提交，请检查状态是否正常"
            break
            ;;
        S)
            break
            ;;
        esac
    done
}

#安装链码
function installCC() {
    cd ${root}/peer

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    echo
    echo "正在安装链码"
    install_ret=$(peer lifecycle chaincode install ../${chaincode_label}.tar.gz 2>&1)
    if [ $? != "0" ]; then
        echo
        echo $install_ret
        echo
        echo "程序异常结束"
        exit 1
    fi
    echo "链码安装成功"

    pos=$(echo ''${install_ret}'' | awk -F ': ' '{printf "%d", length($0)-length($NF)}')
    writePackageId ${install_ret:${pos}}
}

#打包链码
function packageCC() {
    cd ${root}

    #安装 git
    sudo apt install git -y

    pos=$(echo "${chaincode_URL}" | awk -F '/' '{printf "%d", length($0)-length($NF)}')
    declare -i len=$(echo "${chaincode_URL}" | awk -F '.git' '{printf "%d", length($0)-length($NF)}')-$pos-4
    git_name=${chaincode_URL:${pos}:len}

    #判断目录是否存在
    checkDiction ${chaincode_path}
    checkDiction ./${git_name}

    git clone ${chaincode_URL}
    checkRet $?

    if [ $(ls ${chaincode_path} | wc -w) = "0" ]; then
        rm -rf ${chaincode_path}
        checkRet $?
        mv ./${git_name} ${chaincode_path}
        checkRet $?
    fi

    cd ${chaincode_path}
    GO111MODULE=on go mod vendor
    cd ${root}/peer

    echo
    echo "正在打包链码"
    peer lifecycle chaincode package ../${chaincode_label}.tar.gz --path ../${chaincode_path} --lang ${chaincode_lang} --label ${chaincode_label}
    checkRet $?
    echo "链码打包完成"
}

#部署链码
function deployCC() {
    packageCC
    installCC
    approveCC
}

#调用链码
function invoke() {
    cd ${root}/peer

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    args=""
    for j in $(ls ../organizations/tlscacerts -1); do
        if [ ${j} != "tlsca.${order_org_name}-cert.pem" ]; then
            args=${args}" --peerAddresses ${j} --tlsRootCertFiles ../organizations/tlscacerts/${j}"
        fi
    done

    peer chaincode invoke -o ${order_name}:${order_port} --tls --cafile $ORDERER_TLSCA -C ${channelID} -n ${chaincode_name} ${args} -c '{"Args":['${chaincode_invoke_args}']}'
    checkRet $?
}

#查询链码
function query() {
    cd ${root}/peer

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    peer chaincode query -C ${channelID} -n ${chaincode_name} -c '{"Args":['${chaincode_invoke_args}']}'
    checkRet $?
}

#获取通道信息
function getChannelInfo() {
    cd ${root}/peer

    export CORE_PEER_TLS_ENABLED=true
    export CORE_PEER_LOCALMSPID=${MSPID}
    export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/tls/ca.crt
    export CORE_PEER_MSPCONFIGPATH=${PWD}/../organizations/peerOrganizations/${org_name}/users/Admin@${org_name}/msp
    export CORE_PEER_ADDRESS=${peer_name}:${peer_port}
    export ORDERER_TLSCA=${PWD}/../organizations/tlscacerts/tlsca.${order_org_name}-cert.pem

    echo
    peer channel getinfo -c ${channelID}
    checkRet $?
}

#环境变量配置命令
function getDefaultPath() {
    cd ${root}

    echo "环境变量配置命令"
    echo
    echo 'export PATH='${path}':$PATH'
    echo 'export CORE_PEER_TLS_ENABLED=true'
    echo 'export CORE_PEER_LOCALMSPID='${MSPID}''
    echo 'export CORE_PEER_TLS_ROOTCERT_FILE='${PWD}'/organizations/peerOrganizations/'${org_name}'/users/Admin@'${org_name}'/tls/ca.crt'
    echo 'export CORE_PEER_MSPCONFIGPATH='${PWD}'/organizations/peerOrganizations/'${org_name}'/users/Admin@'${org_name}'/msp'
    echo 'export CORE_PEER_ADDRESS='${peer_name}':'${peer_port}''
    echo 'export ORDERER_TLSCA='${PWD}'/organizations/tlscacerts/tlsca.'${order_org_name}'-cert.pem'
}

path=""                           #工具路径
channelID="mychannel"             #通道名
order_path=""                     #order 节点 order.sh 脚本路径
default_config="./default.config" #配置文件位置
peer_name=""                      #peer 名称
org_name=""                       #peer 所属 组织 名称
order_name=""                     #order 名称
order_org_name=""                 #order 所属 组织 名称
core="./core.yaml"                #core.yaml 文件位置
MSPID=""                          #MSPID
order_port=""                     #order 节点端口
peer_port=""                      #当前节点端口

#操作模式 0->部署节点 1->设置锚节点 2->部署链码 3->打包链码 4->安装链码 5->审议链码 6->调用链码 7->查询链码 8->获取通道信息 9->环境变量配置命令
MODE="0"
chaincode_URL="https://gitee.com/snitro/chain-code.git" #链码地址
chaincode_path="./chaincode"                            #本地链码存储位置
chaincode_label="chaincode"                             #链码标签
chaincode_lang="golang"                                 #链码语言
chaincode_name="chaincode"                              #链码名称
chaincode_sequence="1"                                  #链码序列号
chaincode_version="1.0"                                 #链码版本号
chaincode_invoke_args=""                                #调用参数
root=${PWD}                                             #根目录

if [ 0"${GOPATH}" != "0" ]; then
    path="${GOPATH}/src/github.com/hyperledger/fabric/fabric-samples/bin/"
fi

while [[ $# -ge 1 ]]; do
    key="$1"
    case $key in
    -h)
        printHelp
        exit 0
        ;;
    -clear)
        path=""
        channelID=""
        order_path=""
        default_config=""
        peer_name=""
        org_name=""
        order_name=""
        order_org_name=""
        core=""
        MSPID=""
        order_port=""
        peer_port=""
        MODE="0"
        chaincode_URL=""
        chaincode_path=""
        chaincode_label=""
        chaincode_lang=""
        chaincode_name=""
        chaincode_sequence=""
        chaincode_version=""
        chaincode_invoke_args=""
        root=${PWD}
        ;;
    -deployPeer)
        MODE="0"
        ;;
    -setAP)
        MODE="1"
        ;;
    -deployCC)
        MODE="2"
        ;;
    -packageCC)
        MODE="3"
        ;;
    -installCC)
        MODE="4"
        ;;
    -approveCC)
        MODE="5"
        ;;
    -invoke)
        MODE="6"
        chaincode_invoke_args="$2"
        shift
        ;;
    -query)
        MODE="7"
        chaincode_invoke_args="$2"
        shift
        ;;
    -getCI)
        MODE="8"
        ;;

    -getDP)
        MODE="9"
        ;;
    -Path)
        path="$2"
        shift
        ;;
    -channelID)
        channelID="$2"
        shift
        ;;
    -order_path)
        order_path="$2"
        shift
        ;;
    -default_config)
        default_config="$2"
        shift
        ;;
    -core)
        core="$2"
        shift
        ;;
    -MSPID)
        MSPID="$2"
        shift
        ;;
    -chaincode_URL)
        chaincode_URL="$2"
        shift
        ;;
    -chaincode_path)
        chaincode_path="$2"
        shift
        ;;
    -chaincode_label)
        chaincode_label="$2"
        shift
        ;;
    -chaincode_lang)
        chaincode_lang="$2"
        shift
        ;;
    -chaincode_name)
        chaincode_name="$2"
        shift
        ;;
    -chaincode_sequence)
        chaincode_sequence="$2"
        shift
        ;;
    -chaincode_version)
        chaincode_version="$2"
        shift
        ;;
    *)
        echo
        echo "Unknown flag: $key"
        echo
        printHelp
        exit 1
        ;;
    esac
    shift
done

#环境变量配置
export PATH=${path}:$PATH

#纠正配置文件位置
while [ ! -f ${default_config} ]; do
    echo "文件 ${default_config} 不存在！"
    echo "请输入正确的配置文件位置"
    read default_config
done

#读取配置文件
line_num=0
for line in $(cat ${default_config}); do
    config_array[${line_num}]=$(echo ${line// /} | tr -d '\r')
    ((line_num++))
done

#识别配置文件
peer_array=(${config_array[1]//:/ })
order_array=(${config_array[0]//:/ })
peer_name=${peer_array[0]}
peer_port=${peer_array[1]}
getOrg $peer_name
order_name=${order_array[0]}
order_port=${order_array[1]}
getOrderOrg $order_name
MSPID=${config_array[2]}

#纠正MSPID
if [ 0"${MSPID}" = "0" ]; then
    echo "请提供MSPID以操作通道"
    read MSPID
fi

case ${MODE} in
0)
    if [ 0"${order_path}" = "0" ]; then
        echo "需要 order_path ,以定位文件位置"
        read order_path
    fi

    clear
    echo "开始部署 ${peer_name}"
    sleep 1s

    if [ "${line_num}" = "4" ]; then
        deployFirstPeer
    else
        deployOtherPeer
    fi
    ;;
1)
    setAnchorPeer
    ;;
2)
    deployCC
    ;;
3)
    packageCC
    ;;
4)
    installCC
    ;;
5)
    approveCC
    ;;
6)
    invoke
    ;;
7)
    query
    ;;
8)
    getChannelInfo
    ;;
9)
    getDefaultPath
    ;;
*)
    echo "未知模式，程序错误"
    exit 1
    ;;
esac
